package contegor.scire.core;

import java.lang.reflect.Method;

import contegor.scire.core.queries.Condition;

/**
 * {@link Attribute} implementation.
 * 
 * @author  Héctor Cirbián Pousa
 */
public class AttributeImpl extends ConceptImpl implements Attribute {
  private static final long serialVersionUID = -2646150991601921735L;

  protected Method generator;

  public AttributeImpl(long creationTime, TransactionId transactionId, IdFragment idFragment, String generatorClass, String generatorMethod) {
    super(creationTime, transactionId, idFragment);
    try {
      Class<?> clss = Class.forName(generatorClass);
      generator = clss.getMethod(generatorMethod, Attribute.class);
      if (!Condition.class.isAssignableFrom(generator.getReturnType()))
        throw new IllegalArgumentException(generatorClass + "#" + generatorMethod + " does not return a Condition.");
    }
    catch (RuntimeException ex) {
      throw ex;
    }
    catch (Exception ex) {
      throw new IllegalArgumentException(ex);
    }
  }

  @Override
  @SuppressWarnings("unchecked")
  public Condition<Concept> createConditionInstance() {
    try {
      return (Condition<Concept>)generator.invoke(null, this);
    }
    catch (Exception ex) {
      throw new RuntimeException(ex.getMessage(), ex);
    }
  }
}
